package com.slz3.business.module.systemManagement.sysUser.steward;

import com.aliyun.oss.common.utils.StringUtils;
import com.slz3.business.frame.enums.AuditFlag;
import com.slz3.business.frame.enums.ValidFlag;
import com.slz3.business.frame.module.Repositorys;
import com.slz3.business.frame.module.Services;
import com.slz3.business.frame.response.RR;
import com.slz3.business.frame.util.VE;
import com.slz3.business.module.systemManagement.sysOrganize.domain.SysOrganize;
import com.slz3.business.module.systemManagement.sysUser.domain.SysUser;
import com.slz3.business.module.systemManagement.sysUser.domain.SysUserCondition;
import org.springframework.util.CollectionUtils;
import org.springframework.web.reactive.function.server.ServerRequest;

import java.util.List;
import java.util.Optional;
import java.util.UUID;

public class SysUserResSafeValid {

    static boolean add(ServerRequest req, SysUser sysUser) {
        // T-O-D-O 唯一约束
        Optional<SysOrganize> single = Services.sysOrganize.single(sysUser.getOrganizeUuid());
        if (!single.isPresent()){
            RR.resDetail(req).put(RR.SAFE_VALID,  "查询不到对应的组织信息");
        }
        SysUser param = new SysUser();
        param.setSerName(sysUser.getSerName());
        List<SysUser> users = Services.sysUser.findAll(param);
        if (!CollectionUtils.isEmpty(users)){
            RR.resDetail(req).put(RR.SAFE_VALID,  "该用户名已经存在");
        }
//        if (CollectionUtils.isEmpty(sysUser.getRoleUuids())){
//            req.attributes().put(RR.SAFE_VALID, "请至少选择一个角色");
//        }
//        if (Services.sysRoleUser.findAllByUuids(sysUser.getRoleUuids()).size() != sysUser.getRoleUuids().size()){
//            req.attributes().put(RR.SAFE_VALID, "请选择有效的角色");
//        }
        return RR.hasResErr(req);
    }

    /**
     * known: params uuids is not null
     */
    static final boolean delete(ServerRequest req, SysUserCondition sysUserCondition) {
        List<SysUser> sysUserList = Services.sysUser.findAllByUuids(sysUserCondition.getUuidList());
        if (sysUserList.stream().filter(
                item -> AuditFlag.isApproved(item.getSerAudit()) || AuditFlag.isReject(item.getSerAudit()) || ValidFlag.isDel(item.getValidFlag())
        ).count() > 0) {
            RR.resDetail(req).put(RR.SAFE_VALID, "不能删除【已审核】或【被驳回】或【被删除】状态的记录");
        } else {
            req.attributes().put(RR.RELAY_SAFE_VALID, sysUserList);
        }
        return RR.hasResErr(req);
    }

    static boolean modify(ServerRequest req, SysUser sysUser) {
        Optional<SysUser> sysUserDB = Services.sysUser.single(sysUser.getUuid());
        if (sysUserDB.isPresent()) {
            req.attributes().put(RR.RELAY_SAFE_VALID, sysUserDB.get());
        } else {
            RR.resDetail(req).put(RR.SAFE_VALID, "cannot find entity by request uuid");
        }
        Optional<SysOrganize> single = Services.sysOrganize.single(sysUser.getOrganizeUuid());
        if (!single.isPresent()){
            RR.resDetail(req).put(RR.SAFE_VALID,  "查询不到对应的组织信息");
        }
        SysUser param = new SysUser();
        param.setSerName(sysUser.getSerName());
        List<SysUser> users = Services.sysUser.findAll(param);
        if (!CollectionUtils.isEmpty(users) &&
                users.stream().anyMatch(item -> !item.getUuid().equals(sysUser.getUuid()))){
            RR.resDetail(req).put(RR.SAFE_VALID,  "该用户名已经存在");
        }
//        if (CollectionUtils.isEmpty(sysUser.getRoleUuids())){
//            req.attributes().put(RR.SAFE_VALID, "请至少选择一个角色");
//        }
//        if (Services.sysRoleUser.findAllByUuids(sysUser.getRoleUuids()).size() != sysUser.getRoleUuids().size()){
//            req.attributes().put(RR.SAFE_VALID, "请选择有效的角色");
//        }
        return RR.hasResErr(req);
    }

    static boolean info(ServerRequest req) {
        Optional<SysUser> sysUserDB = Services.sysUser.single(UUID.fromString(req.queryParam("uuid").get()));
        if (VE.isEmpty(sysUserDB)) {
            RR.resDetail(req).put(RR.SAFE_VALID, "cannot find entity by request uuid");
        } else {
            req.attributes().put(RR.RELAY_SAFE_VALID, sysUserDB.get());
        }
        return RR.hasResErr(req);
    }

    /**
     * known: params uuids is not null
     */
    static final boolean auditSubmit(ServerRequest req, SysUserCondition sysUserCondition) {
        List<SysUser> sysUserList = Services.sysUser.findAllByUuids(sysUserCondition.getUuidList());
        if (sysUserList.stream().filter(
                item -> !AuditFlag.isWaitSubmit(item.getSerAudit()) || ValidFlag.isDel(item.getValidFlag())
        ).count() > 0) {
            RR.resDetail(req).put(RR.SAFE_VALID, "不能审核【被删除】状态或不是【待提交】状态的记录");
        } else {
            req.attributes().put(RR.RELAY_SAFE_VALID, sysUserList);
        }
        return RR.hasResErr(req);
    }

    /**
     * known: params uuids is not null
     */
    static final boolean audit(ServerRequest req, SysUserCondition sysUserCondition) {
        List<SysUser> sysUserList = Services.sysUser.findAllByUuids(sysUserCondition.getUuidList());
        if (sysUserList.stream().filter(
                item -> !AuditFlag.isWaitAudit(item.getSerAudit()) || ValidFlag.isDel(item.getValidFlag())
        ).count() > 0) {
            RR.resDetail(req).put(RR.SAFE_VALID, "不能审核【被删除】状态或不是【待审核】状态的记录");
        } else {
            req.attributes().put(RR.RELAY_SAFE_VALID, sysUserList);
        }
        return RR.hasResErr(req);
    }

    /**
     * known: params uuids is not null
     */
    static final boolean auditReject(ServerRequest req, SysUserCondition sysUserCondition) {
        List<SysUser> sysUserList = Services.sysUser.findAllByUuids(sysUserCondition.getUuidList());
        if (sysUserList.stream().filter(
                item -> !AuditFlag.isWaitAudit(item.getSerAudit())||!AuditFlag.isApproved(item.getSerAudit()) || ValidFlag.isDel(item.getValidFlag())
        ).count() > 0) {
            RR.resDetail(req).put(RR.SAFE_VALID, "不能驳回【被删除】状态或不是【待审核】或【已审核】状态的记录");
        } else {
            req.attributes().put(RR.RELAY_SAFE_VALID, sysUserList);
        }
        return RR.hasResErr(req);
    }

    
    static final boolean usingEnable(ServerRequest req, SysUserCondition sysUserCondition) {
        List<SysUser> sysUserList = Services.sysUser.findAllByUuids(sysUserCondition.getUuidList());
        if (sysUserList.stream().filter(
                item -> !AuditFlag.isWaitAudit(item.getSerAudit()) || ValidFlag.isDel(item.getValidFlag())
        ).count() > 0) {
            RR.resDetail(req).put(RR.SAFE_VALID, "不能启用【被删除】状态");
        } else {
            req.attributes().put(RR.RELAY_SAFE_VALID, sysUserList);
        }
        return RR.hasResErr(req);
    }


    static final boolean usingDisable(ServerRequest req, SysUserCondition sysUserCondition) {
        List<SysUser> sysUserList = Services.sysUser.findAllByUuids(sysUserCondition.getUuidList());
        if (sysUserList.stream().filter(
                item -> !AuditFlag.isWaitAudit(item.getSerAudit()) || ValidFlag.isDel(item.getValidFlag())
        ).count() > 0) {
            RR.resDetail(req).put(RR.SAFE_VALID, "不能禁用【被删除】状态");
        } else {
            req.attributes().put(RR.RELAY_SAFE_VALID, sysUserList);
        }
        return RR.hasResErr(req);
    }


}
